package cn.jy.boot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();
        // authorities  role 只能选一个
        userDetailsManager.createUser(User.withUsername("admin").password("{noop}123").roles("userAdmin").build());
//        userDetailsManager.createUser(User.withUsername("admin").password("{noop}123").authorities("userAdmin").build());
        userDetailsManager.createUser(User.withUsername("product").password("{noop}123").roles("productManager").build());
        userDetailsManager.createUser(User.withUsername("order").password("{noop}123").roles("orderManager").build());
        return userDetailsManager;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService());
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //授权 角色 权限
        /**
         * permitAll  放行
         */
        http.authorizeRequests()
                .mvcMatchers("/","/index").permitAll()
                //hasRole("角色名字ROLE_productManager,不需要加ROLE前缀的，严格区分大小写（ABC abc 是两个角色）")
//                .mvcMatchers("/pro/**").hasRole("productManager")
//                .antMatchers("/pro/**").hasAnyRole("userAdmin","productManager")
//                .mvcMatchers("/order/**").anonymous()
//                .antMatchers("/pro/**").hasAuthority("productManager")
//                .antMatchers("/pro/**").hasAnyAuthority("pro","userAdmin")
//                .antMatchers("/demo/index").permitAll()
//                .regexMatchers()//可以写正则表达式
                .anyRequest().authenticated();
        //登录认证
        http.formLogin()
//                .defaultSuccessUrl("/index",true)
              ;

        //注销
//        http.logout();
        //异常处理
//        http.exceptionHandling();
        //开启会话管理
//        http.sessionManagement();
        //开启记住我
//        http.rememberMe();
        //关闭跨域请求的保护
        http.csrf().disable();

    }
}
